#![allow(dead_code)]
use crate::domain::CountNum;
use crate::domain::RecommendBean;
use crate::domain::RecommendQueryRequest;
use crate::serviceimpl::init_db;
use crate::utils::string_util::is_digit;
use crate::utils::string_util::right_find_under_line;
use sqlx::Error;
use substring::Substring;
use std::format;

pub async fn activate_all_recommends(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let recommends = &app_state.await.context.recommends;
	let vecter_iterator = ids.iter();
	for recommend_id in vecter_iterator {
		let _ = recommends.activate_recommend(*recommend_id).await;
	}
	Ok(0)
}

pub async fn activate_recommend(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.activate_recommend(id).await
}

pub async fn add_recommend(recommend:RecommendBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.add_recommend(recommend).await
}

pub async fn clone_all_recommends(ids:Vec<i64>) ->Result<u64, Error> {
	let vecter_iterator = ids.iter();
	for recommend_id in vecter_iterator {
		let _ = clone_recommend(*recommend_id).await;
	}
	Ok(0)
}

pub async fn clone_recommend(id:i64) ->Result<u64, Error> {
	let mut recommend = find_recommend_by_id(id).await.unwrap();
	let name = find_recommend_indexed_name(recommend.name).await.unwrap();
	recommend.name = name;
	add_recommend(recommend).await
}

pub async fn count_search_recommends_by_fields_records(recommend_query_request:RecommendQueryRequest) ->Result<CountNum, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.count_search_recommends_by_fields_records(recommend_query_request).await
}

pub async fn delete_all_recommends(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let recommends = &app_state.await.context.recommends;
	let vecter_iterator = ids.iter();
	for recommend_id in vecter_iterator {
		let _ = recommends.delete_recommend(*recommend_id).await;
	}
	Ok(0)
}

pub async fn delete_recommend(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.delete_recommend(id).await
}

pub async fn find_recommend_by_id(id:i64) ->Result<RecommendBean, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.find_recommend_by_id(id).await
}

pub async fn find_recommend_by_name(name:String) ->Result<RecommendBean, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.find_recommend_by_name(name).await
}

pub async fn list_active_recommends() ->Result<Vec<RecommendBean>, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.list_active_recommends().await
}

pub async fn list_all_recommends() ->Result<Vec<RecommendBean>, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.list_all_recommends().await
}

pub async fn search_recommends_by_fields(recommend_query_request:RecommendQueryRequest) ->Result<Vec<RecommendBean>, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.search_recommends_by_fields(recommend_query_request).await
}

pub async fn search_recommends_by_fields_by_page(recommend_query_request:RecommendQueryRequest) ->Result<Vec<RecommendBean>, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.search_recommends_by_fields_by_page(recommend_query_request).await
}

pub async fn soft_delete_all_recommends(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let recommends = &app_state.await.context.recommends;
	let vecter_iterator = ids.iter();
	for recommend_id in vecter_iterator {
		let _ = recommends.soft_delete_recommend(*recommend_id).await;
	}
	Ok(0)
}

pub async fn soft_delete_recommend(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.soft_delete_recommend(id).await
}

pub async fn toggle_one_recommend(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	let recommends = &app_state.await.context.recommends;
	let recommend = recommends.find_recommend_by_id(id).await.unwrap();
	if recommend.active == false {
		let _ = recommends.toggle_recommend(id).await;
	} else {
		let count = recommends.count_active_recommend_records().await.unwrap();
		if count.count_num > 1 {
			let _ = recommends.toggle_recommend(id).await;
		}
	}
	Ok(0)
}

pub async fn toggle_recommend(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.toggle_recommend(id).await
}

pub async fn update_recommend(recommend:RecommendBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.update_recommend(recommend).await
}

pub async fn find_recommend_indexed_name(name:String) ->Result<String, Error> {
	let mut short_name = name.clone();
	if name.contains("_") && is_digit(name.substring(right_find_under_line(name.clone())+1,name.chars().count()).to_string()) {
		short_name = name.substring(0,right_find_under_line(name.clone())).to_string();
	}

	let app_state = init_db();
	let recommends = app_state.await.context.recommends.find_recommend_indexed_name(short_name.clone()).await.unwrap();

	let mut index_num = 1;
	for recommend in recommends {
		let name2 = recommend.name;
		if name2.contains("_") && is_digit(name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).to_string()) {
			let index = name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).parse::<i32>().unwrap();
			if index >= index_num {
				index_num = index + 1;
			}
		}
	}

	let index_name = short_name.clone() + "_" + &index_num.to_string();
	Ok(index_name)
}

pub async fn add_recommend_type(goods_id:i64, recommend_type_id:i64) ->Result<u64, Error> {
	remove_recommend_type(goods_id,recommend_type_id).await.unwrap();
    let app_state = init_db();
	app_state.await.context.recommends.add_recommend_type(goods_id,recommend_type_id).await
}

pub async fn remove_recommend_type(goods_id:i64, recommend_type_id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.recommends.remove_recommend_type(goods_id,recommend_type_id).await
}

pub async fn is_recommend(goods_id:i64,recommend_type_id:i64) ->Result<bool, Error> {
    let app_state = init_db();
	let count = app_state.await.context.recommends.is_recommend(goods_id,recommend_type_id).await.unwrap().count_num;
	if count == 1 {
	   return Ok(true);
	} else {
	   return Ok(false);
	}
}

pub async fn is_scroll(goods_id:i64) ->Result<bool, Error> {
    is_recommend(goods_id,1).await
}

pub async fn is_hot(goods_id:i64) ->Result<bool, Error> {
    is_recommend(goods_id,2).await
}

pub async fn is_new(goods_id:i64) ->Result<bool, Error> {
    is_recommend(goods_id,3).await
}
